﻿using System; 
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Matrix.Common.DBUtility;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.Dto;

namespace Matrix.MaKaYunShang.DAL
{
	public partial class PaymentRecordsDAL : BaseDAL<PaymentRecordsDAL>
	{
		#region 默认方法
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(PaymentRecords model)
		{
			var strSql=new StringBuilder();
			strSql.Append("insert into PaymentRecords(");			
            strSql.Append("OrderId,PrePayId,LocalTransactionId,WechatTransactionId,PayPrice,PayResult,PayResultDescption,AddTime,CallBackTime,FinishPayTime,CallBackContent,PayPlatformId,IpAddress,Longitude,Latitude,IsDelete");
			strSql.Append(") values (");
            strSql.Append("@OrderId,@PrePayId,@LocalTransactionId,@WechatTransactionId,@PayPrice,@PayResult,@PayResultDescption,@AddTime,@CallBackTime,@FinishPayTime,@CallBackContent,@PayPlatformId,@IpAddress,@Longitude,@Latitude,@IsDelete");            
            strSql.Append(") ");            
            strSql.Append(";select @@IDENTITY");		
			SqlParameter[] parameters = {
			            new SqlParameter("@OrderId", SqlDbType.Int,4) ,            
                        new SqlParameter("@PrePayId", SqlDbType.NVarChar,128) ,            
                        new SqlParameter("@LocalTransactionId", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@WechatTransactionId", SqlDbType.NVarChar,32) ,            
                        new SqlParameter("@PayPrice", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@PayResult", SqlDbType.Int,4) ,            
                        new SqlParameter("@PayResultDescption", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@AddTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@CallBackTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@FinishPayTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@CallBackContent", SqlDbType.NVarChar,2000) ,            
                        new SqlParameter("@PayPlatformId", SqlDbType.Int,4) ,            
                        new SqlParameter("@IpAddress", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Longitude", SqlDbType.NVarChar,16) ,            
                        new SqlParameter("@Latitude", SqlDbType.NVarChar,16) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };
			            
            parameters[0].Value = model.OrderId;                        
            parameters[1].Value = model.PrePayId;                        
            parameters[2].Value = model.LocalTransactionId;                        
            parameters[3].Value = model.WechatTransactionId;                        
            parameters[4].Value = model.PayPrice;                        
            parameters[5].Value = model.PayResult;                        
            parameters[6].Value = model.PayResultDescption;                        
            parameters[7].Value = model.AddTime;                        
            parameters[8].Value = model.CallBackTime;                        
            parameters[9].Value = model.FinishPayTime;                        
            parameters[10].Value = model.CallBackContent;                        
            parameters[11].Value = model.PayPlatformId;                        
            parameters[12].Value = model.IpAddress;                        
            parameters[13].Value = model.Longitude;                        
            parameters[14].Value = model.Latitude;                        
            parameters[15].Value = model.IsDelete;            			
            var obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);			
			return obj == null ? 0 : Convert.ToInt32(obj);
		}
				
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(PaymentRecords model)
		{
			var strSql=new StringBuilder();
			strSql.Append("update PaymentRecords set ");
			                                                
            strSql.Append(" OrderId = @OrderId , ");                                    
            strSql.Append(" PrePayId = @PrePayId , ");                                    
            strSql.Append(" LocalTransactionId = @LocalTransactionId , ");                                    
            strSql.Append(" WechatTransactionId = @WechatTransactionId , ");                                    
            strSql.Append(" PayPrice = @PayPrice , ");                                    
            strSql.Append(" PayResult = @PayResult , ");                                    
            strSql.Append(" PayResultDescption = @PayResultDescption , ");                                    
            strSql.Append(" AddTime = @AddTime , ");                                    
            strSql.Append(" CallBackTime = @CallBackTime , ");                                    
            strSql.Append(" FinishPayTime = @FinishPayTime , ");                                    
            strSql.Append(" CallBackContent = @CallBackContent , ");                                    
            strSql.Append(" PayPlatformId = @PayPlatformId , ");                                    
            strSql.Append(" IpAddress = @IpAddress , ");                                    
            strSql.Append(" Longitude = @Longitude , ");                                    
            strSql.Append(" Latitude = @Latitude , ");                                    
            strSql.Append(" IsDelete = @IsDelete  ");            			
			strSql.Append(" where Id=@Id ");
						
			SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@OrderId", SqlDbType.Int,4) ,            
                        new SqlParameter("@PrePayId", SqlDbType.NVarChar,128) ,            
                        new SqlParameter("@LocalTransactionId", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@WechatTransactionId", SqlDbType.NVarChar,32) ,            
                        new SqlParameter("@PayPrice", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@PayResult", SqlDbType.Int,4) ,            
                        new SqlParameter("@PayResultDescption", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@AddTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@CallBackTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@FinishPayTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@CallBackContent", SqlDbType.NVarChar,2000) ,            
                        new SqlParameter("@PayPlatformId", SqlDbType.Int,4) ,            
                        new SqlParameter("@IpAddress", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Longitude", SqlDbType.NVarChar,16) ,            
                        new SqlParameter("@Latitude", SqlDbType.NVarChar,16) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };
						            
            parameters[0].Value = model.Id;                        
            parameters[1].Value = model.OrderId;                        
            parameters[2].Value = model.PrePayId;                        
            parameters[3].Value = model.LocalTransactionId;                        
            parameters[4].Value = model.WechatTransactionId;                        
            parameters[5].Value = model.PayPrice;                        
            parameters[6].Value = model.PayResult;                        
            parameters[7].Value = model.PayResultDescption;                        
            parameters[8].Value = model.AddTime;                        
            parameters[9].Value = model.CallBackTime;                        
            parameters[10].Value = model.FinishPayTime;                        
            parameters[11].Value = model.CallBackContent;                        
            parameters[12].Value = model.PayPlatformId;                        
            parameters[13].Value = model.IpAddress;                        
            parameters[14].Value = model.Longitude;                        
            parameters[15].Value = model.Latitude;                        
            parameters[16].Value = model.IsDelete;                        
            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
		}
				
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			var strSql=new StringBuilder();
			strSql.Append("update PaymentRecords set IsDelete=1 ");
			strSql.Append(" where Id=@Id");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
		}
		
				/// <summary>
		/// 批量删除一批数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			var strSql=new StringBuilder();
			strSql.Append("update PaymentRecords set IsDelete=1 ");
			strSql.Append(" where Id in ("+Idlist + ")  ");
			return DbHelperSQL.ExecuteSql(strSql.ToString()) > 0;
		}
						
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DataSet GetModel(int Id)
		{
			var strSql=new StringBuilder();
			strSql.Append("select Id, OrderId, PrePayId, LocalTransactionId, WechatTransactionId, PayPrice, PayResult, PayResultDescption, AddTime, CallBackTime, FinishPayTime, CallBackContent, PayPlatformId, IpAddress, Longitude, Latitude, IsDelete  ");			
			strSql.Append("  from PaymentRecords ");
			strSql.Append(" where Id=@Id  and IsDelete=0");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;
			
			var ds = DbHelperSQL.Query(strSql.ToString(),parameters);			
			return ds.Tables[0].Rows.Count>0 ? ds : null;
		}
				
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			var strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM PaymentRecords where IsDelete=0 ");
			if(!string.IsNullOrEmpty(strWhere))
			{
				strSql.Append(" and "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}		
		#endregion
		
		#region 扩展方法


        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <returns></returns>
        public List<ViewPaymentRecordsDto> GetPagingList(int pageIndex, int pageSize, string searchKeyWord, string colOrder, ref int totalCount)
        {
            string strWhere = " IsDelete = 0 ";

            if (!string.IsNullOrEmpty(searchKeyWord))
            {
                strWhere += " AND (OrderId LIKE '%" + searchKeyWord + "%' OR PrePayId LIKE '%" + searchKeyWord +
                            "%' OR LocalTransactionId LIKE '%" + searchKeyWord + "%'  OR WechatTransactionId LIKE '%" + searchKeyWord + "%'  OR PayPrice LIKE '%" + searchKeyWord + "%' OR LocalTransactionId LIKE '%" + searchKeyWord + "%'  OR PayResult LIKE '%" + searchKeyWord + "%'  OR AddTime LIKE '%" + searchKeyWord + "%') ";
                if (searchKeyWord.Contains("微信支付"))
                {
                    strWhere = " IsDelete = 0  ";
                    strWhere += " and PayPlatformId LIKE '%" + 1 + "%'";
                }
                else if (searchKeyWord.Contains("货到付款") || searchKeyWord.Contains("现金或水票"))
                {
                    strWhere = " IsDelete = 0  ";
                    strWhere += " and PayPlatformId LIKE '%" + 2 + "%'";
                }
                DateTime dt;
                if (DateTime.TryParse(searchKeyWord,out dt))
                {
                    strWhere = " IsDelete = 0  ";
                    strWhere += " and convert(varchar(10),AddTime,120) LIKE '%" + searchKeyWord + "%'";
                }
                if (searchKeyWord == "男" || searchKeyWord == "女")
                {
                    searchKeyWord = searchKeyWord == "男" ? "1" : searchKeyWord == "女" ? "2" : searchKeyWord;
                    strWhere = " IsDelete = 0  and sex=" + searchKeyWord;
                }
                if (searchKeyWord == "未支付")
                {
                    strWhere = " IsDelete = 0  ";
                    strWhere += " and PayResult LIKE '%" +0+ "%'";  
                }
                if (searchKeyWord == "已支付")
                {
                    strWhere = " IsDelete = 0  ";
                    strWhere += " and PayResult LIKE '%" + 1 + "%'";
                }
                if (searchKeyWord == "支付失败")
                {
                    strWhere = " IsDelete =0  ";
                    strWhere += " and PayResult LIKE '%" + 2 + "%'";
                }
            }
            return GetSimpleListPage<ViewPaymentRecordsDto>(pageIndex, pageSize, "PaymentRecords", null, null, colOrder, strWhere,
                            ref totalCount);
        }

        /// <summary>
        /// 更新订单支付数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
	    public bool UpdateByOrderIdAndLocalTransId(PaymentRecords model)
	    {
            var strSql = new StringBuilder();
            strSql.Append("update PaymentRecords set ");

            strSql.Append(" WechatTransactionId = @WechatTransactionId , ");
            strSql.Append(" PayResult = @PayResult , ");
            strSql.Append(" PayResultDescption = @PayResultDescption , ");
            strSql.Append(" CallBackTime = @CallBackTime , ");
            strSql.Append(" FinishPayTime = @FinishPayTime , ");
            strSql.Append(" CallBackContent = @CallBackContent, ");
            strSql.Append(" PrePayId = @PrePayId ");
            strSql.Append(" where Id=@Id AND OrderId = @OrderId AND LocalTransactionId = @LocalTransactionId ");

            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@OrderId", SqlDbType.Int,4) ,          
                        new SqlParameter("@LocalTransactionId", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@WechatTransactionId", SqlDbType.NVarChar,32) ,            
                        new SqlParameter("@PayResult", SqlDbType.Int,4) ,            
                        new SqlParameter("@PayResultDescption", SqlDbType.NVarChar,50) ,          
                        new SqlParameter("@CallBackTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@FinishPayTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@CallBackContent", SqlDbType.NVarChar,2000) ,            
                        new SqlParameter("@PrePayId", SqlDbType.NVarChar,128)             
              
            };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.OrderId;
            parameters[2].Value = model.LocalTransactionId;
            parameters[3].Value = model.WechatTransactionId;
            parameters[4].Value = model.PayResult;
            parameters[5].Value = model.PayResultDescption;
            parameters[6].Value = model.CallBackTime;
            parameters[7].Value = model.FinishPayTime;
            parameters[8].Value = model.CallBackContent;
            parameters[9].Value = model.PrePayId;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
	    }

		#endregion
	}
}
